package Classes.GameDatas// ActionScript file
{
	import Classes.GameRecords.GR_Build;
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Func;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	
	import UIComponents.IStore;
	import UIComponents.IUseItem;
	
	import flash.display.DisplayObject;

	public class GD_BuildingItem extends GameData implements IUseItem,IStore
	{
		private static var _buildingItemsConfigObj:Object;
		
		private var _buildingItemType:Number;
		private var _buildingItemConfigObj:Object;
		private var _buildingItemNum:int=1;
		private var _icon:ViewIcon;
	
		public function GD_BuildingItem(buildingItemType:Number):void
		{
			this._buildingItemType=buildingItemType;
			this.init();
		}
		
		private function init():void
		{
			this._buildingItemConfigObj=buildingItemsConfigObj[this.buildingItemType];
		}
		
		public static function creat(creatObj:Object):GD_BuildingItem
		{
			var buildingItem:GD_BuildingItem=new GD_BuildingItem(creatObj.buildingItemType);
			if(creatObj.buildingItemNum)buildingItem._buildingItemNum=creatObj.buildingItemNum;
			return buildingItem;
		}
		
		public function useItem():void
		{
			removeChildBeforeAddChild();
			var buildingObj:Object={buildingType:this.buildingType,buildingLv:this.buildingLv,tmp:true};
			var tmpFlag:Boolean=GV_Map.instance.mapData.hasBulidingByFunId(this.buildingType);
			var building:GD_Building_Func=GV_Map.instance.mapData.addBuilding(buildingObj);
			building.setBuildType(BuildType.PLACE);
			building.tmpHasBuildInMap=tmpFlag;
			GV_Map.instance.layerBuildingSet.getBuilding(building.view);
			GD_Player.instance.storeBuildingNumChanged({buildingItemType:this.buildingItemType,value:this.buildingItemNum-1});
		}
		
		public function doSomeThingFun(shopId:Number,itemPrice:Number,moneyType:Number,isDoing:Boolean=true):void
		{
			if(!isDoing)
			{
				VP_errorPanel.instance.setErrorMessage(Language.notEnoughMoney);
				VP_errorPanel.instance.enterFrame();
				GU_Sound.instance.playCommSound("System_Prompt");
				return;
			}
			removeChildBeforeAddChild();
			var shopBuild:Object={buildingType:this.buildingType,buildingLv:this.buildingLv,tmp:true};
			var tmpFlag:Boolean=GV_Map.instance.mapData.hasBulidingByFunId(this.buildingType,true);	
			var shopBuilding:GD_Building_Func=GV_Map.instance.mapData.addBuilding(shopBuild);
			shopBuilding.setBuildType(BuildType.BUY);
			shopBuilding.tmpHasBuildInMap=tmpFlag;
			shopBuilding.shopId=shopId;
			shopBuilding.moneyType=moneyType;
			shopBuilding.bulidPrice=itemPrice;
			GV_Map.instance.layerBuildingSet.getBuilding(shopBuilding.view);
			if(moneyType==0)
			{
				GD_Player.instance.gold-=itemPrice;
			}else if(moneyType==1){
				GD_Player.instance.money-=itemPrice;
			}else if(moneyType==2)
			{
				GD_Player.instance.exchangeBuildItemNum-=itemPrice;
			}else{
				GD_Player.instance.exchangeBuildItemNum-=itemPrice;
			}
		}
		
		private function removeChildBeforeAddChild():void
		{
			while(GV_Map.instance.layerBuildingSet.layerBuilding.numChildren>0)
			{
				var mt:MT_Building_Func=GV_Map.instance.layerBuildingSet.layerBuilding.getChildAt(0) as MT_Building_Func;
				var tmpBuildType:BuildType=mt.buildingFuncData.buildType;
				if(tmpBuildType.changeType==BuildType.BUY)
				{
					GR_Build.instance.deleteBuilding(mt.buildingFuncData);
					GV_Map.instance.layerBuildingSet.layerBuilding.removeChild(mt.buildingFuncData.view);
					mt.buildingFuncData.recoverBuy();
				}else if(tmpBuildType.changeType==BuildType.PLACE){
					GR_Build.instance.deleteBuilding(mt.buildingFuncData);
					GV_Map.instance.layerBuildingSet.layerBuilding.removeChild(mt.buildingFuncData.view);
					mt.buildingFuncData.recoverPlace();
				}
			}
		}
		
		

		/*getter & setter*/
		public static function get buildingItemsConfigObj():Object
		{
			if(!_buildingItemsConfigObj)
			{
				_buildingItemsConfigObj=new Object;
				var buildingConfigDataCSV:String=M_ConfigData.instance.getCSVByName("功能建筑配置表");
				var buildingConfigDataAry:Array=GU_DataTransform.csvTransformObjsAry(buildingConfigDataCSV,16);
				for(var i:int=0;i<buildingConfigDataAry.length;i++)
				{
					var buildingConfigDataObj:Object=buildingConfigDataAry[i];
					_buildingItemsConfigObj[buildingConfigDataObj.zt_id]=buildingConfigDataObj;
				}
			}
			return _buildingItemsConfigObj;
		}

		public function get buildingItemType():Number
		{
			return _buildingItemType;
		}

		public function get buildingItemConfigObj():Object
		{
			return _buildingItemConfigObj;
		}

		public function get buildingItemNum():int
		{
			return _buildingItemNum;
		}

		public function set buildingItemNum(value:int):void
		{
			_buildingItemNum = value;
			this.touchObserve("buildingItemNumber");
		}
		
		public function get buildingType():Number
		{
			return (buildingItemsConfigObj[this.buildingItemType]).zt_func_id;
		}
		
		public function get buildingLv():int
		{
			return this.buildingItemConfigObj.zt_func_greade;
		}
		
		public function get itemPicSourceStr():String
		{
			return this.buildingItemConfigObj.zt_item_source;
		}
		
		public function get itemPicStr():String
		{
			return this.buildingItemConfigObj.zt_item_pic;
		}
		
		public function get itemName():String
		{
			var obj:Object=this.buildingItemConfigObj;
			return this.buildingItemConfigObj.zt_func_name;
		}
		
		public function get itemPic():DisplayObject
		{
			return this.icon;
		}
		
		public function get useEnabled():Boolean
		{
			return true;
		}
		
		public function getStoreItemName():String
		{
			return this.buildingItemConfigObj.zt_func_name;
		}
		public function getStoreItemIcon():DisplayObject
		{
			return this.icon;
		}
		
		public function getStoreItmePic():String
		{
			return this.buildingItemConfigObj.zt_item_pic;
		}
		
		public function get description():String
		{
			return this.buildingItemConfigObj.zt_func_desc;
		}
		public function getStroeItemDes():String
		{
			return this.buildingItemConfigObj.zt_func_desc;
		}
		
		/**
		 * 控制使用按钮是否显示 
		 */
		public function get useBtnVisiable():Boolean
		{
			return true;
		}

		public function get icon():ViewIcon
		{
//			if(!_icon)
//			{
				_icon=ViewIcon.creat({dataTarget:this,creatType:"buildingItem"});
				_icon.scaleX=0.9;
				_icon.scaleY=0.9;
//			}
			return _icon;
		}
		
		public function get itemId():Number
		{
			return this._buildingItemType;
		}

	}
}